package com.sinopac.action.ccroger;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.ccroger.CcrogerVO;
import com.sinopac.model.CustomerApply.CustomerApplyModel;
import com.sinopac.model.GroupManagement.GroupManagementModel;
import com.sinopac.model.ccroger.CcrogerModel;

@SuppressWarnings("serial")
@Namespace("/ccroger")
public class CcrogerAction extends ActionCore{
	
	private CcrogerVO vo;
	//新增關聯帳戶用變數
	private String newRelateCustId;
	//收從後端傳來的訊息參數
	private String errorMessage;
	//關聯帳戶table用變數
	private String relateCustId;
	//使用者角色
	private String userRoles;
	
	@Action(value="index",results={@Result(name="success",location="/form/ccroger/ccroger.jsp")})
	public String execute(){
		userRoles = (String)session.get(USER_ROLES);
		CcrogerModel cm = new CcrogerModel();
		String custId = (String)session.get(CustomersID);
//		System.out.println("custIdcustId:" + custId.replaceAll(new String(" "),new String("")));
		if(StringUtils.isNotBlank(custId)){
			vo = cm.doQuery(custId);
		}
		return SUCCESS;
	}
	
	@Action(value="updateCust",results={@Result(name="success",type="json",params={"root","jsonResult"})})
//										@Result(name="error",type="json",params={"root","jsonResult"})})
	public String updateCust(){
		String custContact = ServletActionContext.getRequest().getParameter("custContact");
		String custPhone = ServletActionContext.getRequest().getParameter("custPhone");
		String custAddress = ServletActionContext.getRequest().getParameter("custAddress");
		String mailArea = ServletActionContext.getRequest().getParameter("mailArea");
		String mailBarCode = ServletActionContext.getRequest().getParameter("mailBarCode");
		CustomerApplyModel cam = new CustomerApplyModel();
		boolean flag = cam.modifyData((String)session.get(CustomersID), custContact, custPhone, custAddress, mailArea, mailBarCode);
		if(flag==false){
//			System.out.println("flagflagflag:"+flag);
//			errorMessage = "客戶資料更新失敗！ 請檢查客戶資料內容是否正確";
			return ERROR;
		}
		setJsonResult(flag?"更新成功":"更新失敗");
		return SUCCESS;
	}
	
	@Action(value="update",results={@Result(name="success",type="chain",location="index")})
	public String update(){
		GroupManagementModel gm = new GroupManagementModel();			
		
		String custId_a = "";
		
		custId_a = (String) session.get(CustomersID);
		
		System.out.println("custId_a:"+custId_a);
		System.out.println("relateCustId:"+relateCustId);
		
		gm.updateRelateAccountList(relateCustId, custId_a);
		session.put(CustomersID, relateCustId);
		return SUCCESS;
	}
	
	@Action(value="addNew",results={@Result(name="success",location="/form/ccroger/ccroger.jsp")})
	public String addNew(){
		GroupManagementModel gm = new GroupManagementModel();
		CcrogerModel cm = new CcrogerModel();
		
		String custId_a = "";
		
		custId_a = (String) session.get(CustomersID);
		
		errorMessage = gm.addNewRelateAccountList(newRelateCustId,custId_a);
		vo = cm.doQuery(custId_a);
//		errorMessage = gm.getErrorMessage();
		
		return SUCCESS;
	}
	
	@Action(value="removeRelated",results={@Result(name="success",type="chain",location="index")})
	public String removeRelated(){
		GroupManagementModel gmm = new GroupManagementModel();
		gmm.deleteRelateAccountList(relateCustId);
		return SUCCESS;
	}
	
	@Action(value="queryToken",results={@Result(name="success",type="json",params={"root","jsonResult"})})
	public String queryToken(){
		String search = ServletActionContext.getRequest().getParameter("search");
		CcrogerModel cm = new CcrogerModel();
		JSONArray ja = cm.getTokenList(search);
		if(!ja.isEmpty()){
			setJsonResult(ja.toString());
		}else{
			setJsonResult("");
		}
		return SUCCESS;
	}
	
	@Action(value="chkToken",results={@Result(name="success",type="json",params={"root","jsonResult"})})
	public String chkToken(){
		String token_esn = ServletActionContext.getRequest().getParameter("token_esn");
		CustomerApplyModel cam = new CustomerApplyModel();
		String message = cam.chkToken((String)session.get(CustomersID), token_esn);
		if(StringUtils.isBlank(message)){
			session.put(TOKEN_ESN, token_esn);
		}
		setJsonResult(message);
		return SUCCESS;
	}
	
	public CcrogerVO getVo() {
		return vo;
	}

	public void setVo(CcrogerVO vo) {
		this.vo = vo;
	}

	public String getNewRelateCustId() {
		return newRelateCustId;
	}

	public void setNewRelateCustId(String newRelateCustId) {
		this.newRelateCustId = newRelateCustId;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getRelateCustId() {
		return relateCustId;
	}

	public void setRelateCustId(String relateCustId) {
		this.relateCustId = relateCustId;
	}

	public String getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(String userRoles) {
		this.userRoles = userRoles;
	}
	
}
